package top.jacktgq.lock.reentrantLock;

import top.jacktgq.utils.LogUtils;

/**
 * 
 * @Title: TestReentrantLock.java 
 * @Package top.jacktgq.lock 
 * @Description: 多线程之固定运行：使用wait和notify
 * @author CandyWall   
 * @date 2020年11月1日 下午5:49:07 
 * @version V1.0
 */
public class Demo5_1_wait_notify {
	private static Object lock = new Object();
	private static boolean t2runned = false;	//表示t2是否运行过
	public static void main(String[] args) {
		Thread t1 = new Thread(() -> {
			synchronized (lock) {
				while(!t2runned) {
					LogUtils.log("Demo5_1_wait_notify", "t2没有运行，进入等待...");
					try {
						lock.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				LogUtils.log("Demo5_1_wait_notify", "线程执行了...");
			}
		}, "t1");
		Thread t2 = new Thread(() -> {
			synchronized (lock) {
				LogUtils.log("Demo5_1_wait_notify", "线程执行了...");
				t2runned = true;
				lock.notify();	//唤醒t1线程
			}
		}, "t2");
		t1.start();
		t2.start();
	}	
}
